import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.ConfigurableEnvironment;

public class CustomYamlApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        try {
            // 使用之前实现的工具类加载 YAML 文件
            PropertySource<?> yamlPropertySource = YamlPropertySourceLoaderUtil.loadYamlAsPropertySource("yaml", "application.yml");
            environment.getPropertySources().addFirst(yamlPropertySource); // 在加载顺序中放在最前面
        } catch (Exception e) {
            throw new RuntimeException("Failed to load YAML configuration file.", e);
        }
    }
}
